#!/usr/bin/perl -w
# Adjust the above line if necessary.
# Written by jfd@Mon Sep 14 16:51:50 WDT 1998
# Update by jfd@Mon Sep 19 15:28:15 CDT 2005
# The script will copy the necessary files for the server
# and adjust the names for the directories and http addresses.
# The argument should be the aquaroot from which to copy.
#
# $aquaroot/scripts/server.pl $aquaroot

# NOTES: disable suexec (by renaming or so) if not needed
#          on the server. It might cause a problem.
# INITIALIZE:

# The substitutions to be made.

# GRACE_LOC is set so gracebat can find the font libs. Normally a full install
# of grace doesn't need it to be set.
%subs_develop = qw(
    WEBMASTER  jurgenfd@gmail.com 

    AQUA_DIR  /Users/jd/Sites
    CGI_DIR   /Library/WebServer/CGI-Executables/
    TMP_DIR   /Users/jd/Sites/tmp
   
    AQUA_LOC  http://localhost/
    CGI_LOC   http://localhost/cgi-bin
    TMP_LOC   http://localhost/~jd/tmp

    GRACE_LOC /Users/jd/progs
    
    PERL_EXE  /usr/bin/perl 
    PERL_FLG  -w    
); 

%subs_madison_pub = qw(
    WEBMASTER  webmaster@bmrb.wisc.edu 

    AQUA_DIR  /home/jurgen/external_html
    CGI_DIR   /home/jurgen/external_html/cgi-bin
    TMP_DIR   /home/jurgen/external_html/tmp
   
    AQUA_LOC  http://tang.bmrb.wisc.edu/~jurgen
    CGI_LOC   http://tang.bmrb.wisc.edu/~jurgen/cgi-bin
    TMP_LOC   http://tang.bmrb.wisc.edu/~jurgen/tmp

    GRACE_LOC /share/linux/src/grace-5.1.17
    
    PERL_EXE  /usr/bin/perl 
    PERL_FLG  -w    
);

%subs_utrecht_pub = qw(
    WEBMASTER  webmaster@bmrb.wisc.edu

    AQUA_DIR  /usr/ruuci12/www/users/jurgen
    CGI_DIR   /nmr10/jurgen/public_html/cgi-bin
    TMP_DIR   /usr/ruuci12/www/users/jurgen/tmp
   
    AQUA_LOC  http://www.nmr.chem.uu.nl/users/jurgen
    CGI_LOC   http://www.nmr.chem.uu.nl/jurgen-cgi-bin
    TMP_LOC   http://www.nmr.chem.uu.nl/users/jurgen/tmp

    PERL_EXE  /usr/share/bin/perl 
    PERL_FLG  -w    
);

%subs_madison_private = qw(
    WEBMASTER     webmaster@bmrb.wisc.edu

    AQUA_DIR      /usr/local/httpd/htdocs/jurgen
    CGI_DIR       /usr/local/httpd/htdocs/jurgen/cgi-bin
    TMP_DIR       /usr/local/httpd/htdocs/jurgen/tmp
 
    AQUA_LOC      http://halfbeak.bmrb.wisc.edu/jurgen
    CGI_LOC       http://halfbeak.bmrb.wisc.edu/jurgen/cgi-bin
    TMP_LOC       http://halfbeak.bmrb.wisc.edu/jurgen/tmp

    PERL_EXE      /usr/bin/perl
    PERL_FLG      -w
);

%subs_heidelberg = qw( 
    WEBMASTER keller@ebi.ac.uk

    AQUA_DIR  /app/pdbcheck/checkserver-1.1/html
    CGI_DIR   /app/pdbcheck/checkserver-1.1/cgi-bin
    TMP_DIR   /app/pdbcheck/checkserver-1.1/html/tmp

    AQUA_LOC  http://biotech.embl-heidelberg.de:8400
    CGI_LOC   http://biotech.embl-heidelberg.de:8400/cgi-bin
    TMP_LOC   http://biotech.embl-heidelberg.de:8400/tmp

    PERL_EXE     /usr/pub/bin/perl
    PERL_FLG     -w
);

# Select here the one we want to make
#%subs = %subs_utrecht_pub;
#%subs = %subs_heidelberg;
#%subs = %subs_madison_private;
#%subs = %subs_madison_pub;
%subs = %subs_develop;

# NO CHANGES REQUIRED AFTER THIS LINE

# Just use them to prevent compiler warnings
%subs_bogus = %subs_utrecht_pub;
%subs_bogus = %subs_heidelberg;
%subs_bogus = %subs_madison_private;
%subs_bogus = %subs_madison_pub;
%subs_bogus = %subs_develop;

foreach $sub ( keys %subs ) {
    printf ("[%50s][%50s]\n", $sub, $subs{$sub});
}

# The directories in which all files will be adjusted

#@dirs = qw ( server cgi ); #normal usage

$cgi_dir    = $subs{"CGI_DIR"}  . "/Aqua/";
$server_dir = $subs{"AQUA_DIR"} . "/Aqua/";
$tmp_dir    = $subs{"TMP_DIR"}  . "/Aqua/";


# Program

get_arguments();
if ( 1 ) {
    make_file_structure();
} else {
    $Q="/localhome/jurgen/aquad";
    $CGI="/usr/local/httpd/htdocs/jurgen/cgi-bin";
    `cp $Q/cgi/viol_chk.*  $CGI/Aqua/`;    
}
make_substitutions();
exit(0);



sub get_arguments {
    if ( defined($ARGV[0]) ) {
        $number_of_arguments = @ARGV;
        if ( $number_of_arguments != 1 ) {
            end_error ("Bad nummer of arguments [$number_of_arguments] supplied, exiting"); 
        }
    } else {
        end_error ("No arguments supplied, exiting\n");
    }

    $base = $ARGV[0];
}



sub make_file_structure {

# cgi-bin dir
    safe_make_dir( $cgi_dir ); 
    ##print "DEBUG: doing: cp $base/cgi/* $cgi_dir";
    if ( `cp $base/cgi/* $cgi_dir` ) {
        print "Unexpected output\n";
    }
    
# tmp dir
# first the dir above

    $dir = $subs{"TMP_DIR"};
    if ( ! (-e $dir ) ) {
        safe_make_dir( $dir );
    }
     
    safe_make_dir( $tmp_dir ); 
# with logs dir
    $logs_dir = $tmp_dir . "logs/";
    safe_make_dir( $logs_dir );

# writable to the world (this might be a security problem)
# before i had it set to 01773 but that did not work in Utrecht
# although it worked on halfbeak.
# but not removable so set the sticky bit
    chmod ( 01777, $tmp_dir, $logs_dir ) ||
    end_error("can not change permissions for tmp dir[s]\n$!");

# aqua dirs

# first the dir above

    safe_make_dir( $server_dir );

    @aqua_server_dirs = qw ( bin data doc html scripts server src aqsetup );
    
    foreach $dir ( @aqua_server_dirs ) {
        if ( `cp -r $base/$dir $server_dir` ) {
            end_error("Unexpected output\n");
        }
    }
}


sub make_substitutions {

    @dirs = ( $cgi_dir, $server_dir . "server" );
#    @dirs = ( $server_dir . "server" );
    
    foreach $dir_item ( @dirs ) {
    
    $dir = $dir_item;
    
    if ( !(-e $dir) ) {
        end_error ("Dir: [$dir] not existing");     
    }
    
    chdir $dir ||
        end_error ("Can not change to dir: [$dir]");    
    print ("In dir: $dir\n");
    
    @files = ( <*.html>,  <*.pl>, <*.csh> );
    
    if ( @files ) {
        foreach $file ( @files ) {
            printf ("Adjusting file: [%20s] ", $file);
            adjust_file ( $file );
            }
        }
    }
    printf ("Finished\n");    
}


sub adjust_file {

    my ( $file ) = @_;
    my ( $tmp_file ) = $file . ".tmp";
    
    my ( $count ) = 0;
    
    open (FILE, "$file" ) ||
    end_error("can not open a readable file: [$file]\n$!");

    if ( -x $file ) {
        $executable = "true";
    } else {
        $executable = "false";  
    }
    
    open (TMP_FILE, ">$tmp_file" ) ||
    end_error("can not open a writable file: [$tmp_file]\n$!");
        
    while ( <FILE> ) {
        foreach $sub ( keys %subs ) {
            if ( $_ =~ $sub ) {
                ##printf "DEBUG:%15s by %15s: %20\n", $sub, $subs{$sub}, $_;
                $count++;
                s/$sub/$subs{$sub}/g;
            }
        }
        print TMP_FILE $_;
    }
    
    close ( TMP_FILE );
    close ( FILE );

    printf ("substituted %d occurances\n", $count);

    unlink ($file);
    rename ($tmp_file, $file) ||
        end_error("can not rename $tmp_file to $file\n$!");

    if ( $executable eq "true" ) {
        chmod ( 0777, $file) ||
            end_error("can not change permissions for $file\n$!");
    }   
}


sub end_error {
    my ( $message ) = @_;

    print ("\n\nERROR: $message\n\n");

    exit(-1);
}

sub higher_dir {
    
    my ( $dir ) = @_;
    my ( $higher_dir ) = "";
    
    @dirs = split /\//, $dir;

    # checks for at least two slashes /a/b/

    if ( @dirs < 2 ) {
    end_error("no two slashes in dir name");
    }
    
    pop @dirs;
    
    foreach $dir ( @dirs ) {
        if ( $dir ne "" ) { 
            $higher_dir = $higher_dir . "/" . $dir;
        }
    }

    $higher_dir = $higher_dir . "/";
    
    return ( $higher_dir );
}


sub safe_make_dir {

    my ( $dir ) = @_;
    my ( $higher_dir );
    
    $higher_dir =  higher_dir( $dir );
    
    if ( !( -e $higher_dir ) ) {
        end_error("Directory not present: $higher_dir \n");
    }

    if ( -e $dir ) {
        remove_dir( $dir );
    } 

    print "Making directory: $dir\n";
    mkdir( $dir, 0755 ) ||  
    end_error ("can not create dir: [$dir]\n$!");    
}


sub remove_dir {

    my( $dir ) = @_;
    my( $file, @files, $sub_dir );

    print "Deleting directory: $dir\n";
    
    chdir $dir ||
    end_error ("Can not change to dir: [$dir]");    
    
    @files = ( <*>, <.[A-z]*> );
    
    if ( @files ) {
        foreach $file ( @files ) {
            if ( -d $file ) {
                $sub_dir = $dir . $file . "/";
                remove_dir ( $sub_dir );
            } else {
            unlink ( $file ) ||
                end_error ("Can not unlink file: [$file]");
            }
        }
    }

# stepping back
    chdir ".." ||
        end_error ("Can not change to dir: [..]");  
    
    rmdir( $dir ) ||
        end_error ("can not remove dir: [$dir]\n$!");    
    
}

